// pages/articles/articles.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      mid: '2247484003',
      sn: '7a27134309af2910b64c70b3e7ed548d',
      chksm: 'e884ca88dff3439ec126265c84d10f4be348800c48334ba4b8659ea69a863e5ac1bb523fa33f',
      imgUrl: 'https://mmbiz.qpic.cn/mmbiz_jpg/Y0BmhoOF5I4eewGeM3kf0sG5F0Utks908eHRXNXnEbEW9HNoLXBXmibOLTUzuLGTibGFGgFicSVXrJfiaLkuMsUUgw/640?wxfrom=5&wx_lazy=1&wx_co=1'
    }, {
      id: 1,
      type: 'image',
      mid: '2247483981',
      sn: '5a8c0d03fb7077f265ee7d26b7be0e24',
      chksm: 'feafa963c9d82075e59c18b0fab2d77bf14a8f5c5b5a06158506f61d83f82e9b59fbc696e529',
      imgUrl: 'https://mmbiz.qpic.cn/mmbiz_jpg/Y0BmhoOF5I5FicFqIubouzTTL7icx2ILd9Ka78AeBTt7THbep8SFlxFh8ISKJhHImIKS6qUib9VVkhVCHzn93UNFg/0?wx_fmt=jpeg',
},{
      id: 2,
      type: 'image',
      mid: '2247483877',
      sn: 'de6a331bfac4c068818dced6d64a5d50',
      chksm: 'feafaacbc9d823dd8b57469b5e7fb29322adbbbff0607ab91cdd2b7c13694aeee716a4e429c3',
      imgUrl: 'https://mmbiz.qpic.cn/mmbiz_jpg/Y0BmhoOF5I5FicFqIubouzTTL7icx2ILd93prTcRGlONcGmFzQtOEsgpWiaYUD3GlzvQQQrtYSNl64VzQQCZx7stw/0?wx_fmt=jpeg',
},
],
    
    articleList: [{
      id: 0,
      tag: '报告',
      title: '2019-2020中国开发者调查报告',
      author: '姬发',
      view: 168,
      star: 480,
      comment: 25,
      createTime: '2022-05-10 20:00:00',
      avatar: 'https://s3.bmp.ovh/imgs/2022/05/10/eba5fa417422e102.jpg',
      mid: '2247484003',
      sn: '7a27134309af2910b64c70b3e7ed548d',
      chksm: 'e884ca88dff3439ec126265c84d10f4be348800c48334ba4b8659ea69a863e5ac1bb523fa33f',
      imgUrl: 'https://mmbiz.qpic.cn/mmbiz_jpg/Y0BmhoOF5I4eewGeM3kf0sG5F0Utks908eHRXNXnEbEW9HNoLXBXmibOLTUzuLGTibGFGgFicSVXrJfiaLkuMsUUgw/640?wxfrom=5&wx_lazy=1&wx_co=1'
    }, {
      id: 1,
      tag: '修行',
      title: '推荐自学网站',
      author: '姬发',
      view: 568,
      star: 99,
      comment: 35,
      createTime: '2022-05-11 10:00:00',
      avatar: 'https://s3.bmp.ovh/imgs/2022/05/10/eba5fa417422e102.jpg',
      mid: '2247483981',
      sn: '5a8c0d03fb7077f265ee7d26b7be0e24',
      chksm: 'feafa963c9d82075e59c18b0fab2d77bf14a8f5c5b5a06158506f61d83f82e9b59fbc696e529',
      imgUrl: 'https://mmbiz.qpic.cn/mmbiz_jpg/Y0BmhoOF5I5FicFqIubouzTTL7icx2ILd9Ka78AeBTt7THbep8SFlxFh8ISKJhHImIKS6qUib9VVkhVCHzn93UNFg/0?wx_fmt=jpeg',
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 1000
    })
    // 初始化towerSwiper 传已有的数组名即可
    this.towerSwiper('swiperList');
  },

  bindViewTap: function(e) {
    console.log('e',e.currentTarget)
    wx.navigateTo({
      url: `/pages/article/article?mid=${e.currentTarget.dataset.mid}&sn=${e.currentTarget.dataset.sn}&chksm=${e.currentTarget.dataset.chksm}`,
    })
  },

  // cardSwiper
  cardSwiper: function(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper: function(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove: function(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd: function(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})